package mk.ukim.finki.studentguide;

import java.util.List;

import mk.ukim.finki.studentguide.xmlconfiguration.Building;
import mk.ukim.finki.studentguide.xmlconfiguration.Level;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class SearchRoom extends Activity {

	private String array_spinner[];
	private int selectedBuilding;
	public List<Level> lstLevels;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_room);
        Building building = ((MyApplication) this.getApplication()).getLstBuildings().get(selectedBuilding);
        lstLevels = building.getLstLevels();
	    array_spinner=new String[4];
	    int idx=0;
	    for(int i=0; i<lstLevels.size();i++){
	    	Level l = lstLevels.get(i);
	        for(int j=0;j<l.getLstRooms().size();j++){
	        	array_spinner[idx]=l.getLstRooms().get(j).getTitle();
	        	idx++;
	        }
	    	
	    }

        final Spinner s = (Spinner) findViewById(R.id.spinnerSearchRoom);
        ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(this, android.R.layout.simple_spinner_item, array_spinner);
        s.setAdapter(adapter);
        
        final Button button = (Button) findViewById(R.id.btnSearchRoom);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	goToShowBuilding();
            }
        });

    }
    
    public void goToShowBuilding(){
    	 Spinner s = (Spinner) findViewById(R.id.spinnerSearchRoom);
    	 ((MyApplication) this.getApplication()).setSearchedRoomName(s.getSelectedItem().toString());
    	 ((MyApplication) this.getApplication()).setComingFromSearch(true);
    	 Intent intent=new Intent(this,ShowBuilding.class);
	     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	     this.startActivity(intent);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            Intent intent = new Intent(this, StudentGuideMapActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
            startActivity(intent);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
